package com.alogic.commonmark.html;

import com.anysoft.util.*;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.NodeRenderer;
import org.commonmark.renderer.html.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;

/**
 * 内部html处理插件
 *
 * @since  1.6.13.16 [20200910 duanyy] <br>
 */
public class InlineHtmlExtension implements Parser.ParserExtension, HtmlRenderer.HtmlRendererExtension, XMLConfigurable, Configurable {

    /**
     * logger of slf4j
     */
    protected final Logger LOG = LoggerFactory.getLogger(InlineHtmlExtension.class);
    protected String filterId = "html.rich";

    @Override
    public void configure(Properties p) {
        filterId = PropertiesConstants.getString(p,"filterId",filterId);
    }

    @Override
    public void configure(Element root, Properties p) {
        XmlElementProperties props = new XmlElementProperties(root,p);
        configure(props);
    }

    @Override
    public void extend(Parser.Builder builder) {
        // nothing to do
    }

    @Override
    public void extend(HtmlRenderer.Builder builder) {
        builder.nodeRendererFactory(new HtmlNodeRendererFactory() {
            @Override
            public NodeRenderer create(HtmlNodeRendererContext htmlNodeRendererContext) {
                return new InlineHtmlNodeRenderer(htmlNodeRendererContext,filterId);
            }
        });
    }
}